package com.rec.smile.ui;

import static com.rec.smile.ImageUtil.actImage;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

public class Actor {

	protected int x, y;
	
	protected int hp;
	
	protected int orihp;

	private boolean addX, addY;
	
	private boolean walking = true;
	
	protected int hurt;
	
	protected Image pic;

	public Actor(int x, int y) {
		super();
		this.x = x;
		this.y = y;
		hp = (int) (100 + Math.random() *10000);
		orihp = hp;
		pic = actImage;
	}

	public void draw(Graphics g) {
		g.drawImage(pic, x, y, null);
		g.setColor(Color.GREEN);
		g.fillRect(x, y-5, pic.getWidth(null) * 2, 5);
		g.setColor(Color.red);
		if(hurt > 0){
			g.drawString(""+hurt, x, y-10);
			hurt = 0;
		}
		g.fillRect(x, y-5, (int)(pic.getWidth(null) * 2* (1 - 1.0f * hp/orihp)), 5);
	}

	public void walk() {
		if(!walking)
			return ;
		if (x > CrPanel.endX) {
			addX = false;
		} else if (x < CrPanel.startX) {
			addX = true;
		}

		if (y > CrPanel.endY) {
			addY = false;
		} else if (y < CrPanel.startY) {
			addY = true;
		}
		if (addX) {
			x += 2;
		} else {
			x -= 2;
		}

		if (addY) {
			y += 2;
		} else {
			y -= 2;
		}

	}

	public boolean meet(Actor actor) {
		if(!walking){
			return true;
		}
		double dis = Math.sqrt((actor.x - x) * (actor.x - x) + (actor.y - y) * (actor.y - y));
		if(dis < 144){
			stopWalking();
			actor.stopWalking();
			return true;
		}
		return false;
	}

	public boolean descHp(int d) {
		hurt = d;
		if(d >=hp ){
			return true;
		}
		hp-=d;
		return false;
	}

	public void startWalking() {
		walking = true;
	}

	protected void stopWalking() {
		walking = false;
	}
}
